package cn.yudao.ymcc.security.config;

import cn.yudao.ymcc.domain.Permission;
import cn.yudao.ymcc.mapper.PermissionMapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

import java.util.List;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private PermissionMapper permissionMapper;

     //提供用户信息，这里没有从数据库查询用户信息，在内存中模拟
//    @Bean
//    public UserDetailsService userDetailsService(){
//        InMemoryUserDetailsManager inMemoryUserDetailsManager =
//        new InMemoryUserDetailsManager();
//        inMemoryUserDetailsManager.createUser(User.withUsername("zs").password("123").authorities("admin").build());
//        return inMemoryUserDetailsManager;
//    }
  

    //密码编码器：不加密
//    @Bean
//    public PasswordEncoder passwordEncoder(){
//        return NoOpPasswordEncoder.getInstance();
//    }

    //密码编码器：加密
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    //配置认证管理器，授权模式为“poassword”时会用到
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    // 测试一下BCryptPasswordEncoder加密和比较
//    public static void main(String[] args) {
//        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
//        String encode1 = bCryptPasswordEncoder.encode("123"); // 加密
//        String encode = bCryptPasswordEncoder.encode("123"); // 加密
//        boolean matches = bCryptPasswordEncoder.matches("123", encode1); // 判断
//        boolean matche = bCryptPasswordEncoder.matches("123", encode);
//        System.out.println(matches);
//        System.out.println(matche);
//        System.out.println(encode);
//        System.out.println(encode1);
//    }
    
    //授权规则配置
    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        List<Permission> permissions = permissionMapper.selectList(null);
//        for(Permission permission : permissions){
//            System.out.println(permission.getResource()+" - "+permission.getSn());
//            //如： /employee/list    需要     employee:list 权限才能访问
//            http.authorizeRequests().antMatchers(permission.getResource()).hasAuthority(permission.getSn());
//        }

        http.authorizeRequests()                                //授权配置
                .antMatchers("/login/common").permitAll()  //登录路径放行
                .anyRequest().authenticated()                   //其他路径都要认证之后才能访问
                .and().formLogin()                              //允许表单登录
//                .successForwardUrl("/login/success")             // 设置登陆成功页
                .and().logout().permitAll()                    //登出路径放行 /logout
                .and().csrf().disable();                        //关闭跨域伪造检查
    }
}